package com.woniuxy.oa.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.entity.Notice;
import com.woniuxy.oa.entity.PageBean;
import com.woniuxy.oa.entity.Status;
import com.woniuxy.oa.service.EmployeeService;
import com.woniuxy.oa.service.NoticeService;
import com.woniuxy.oa.utils.WebSourceToFileUtil;

@Controller
public class NoticeController {

	@Autowired
	NoticeService noticeService;
	
	@Autowired
	EmployeeService employeeService;

	@RequestMapping("/noticeAdd.html")
	public String toNoticePage() {
		return "/system/noticeAdd.html";
	}

	@RequestMapping("/notice/add")
	public ModelAndView putNotice(Notice notice, MultipartFile noticeFile)
			throws IllegalStateException, IOException {
		System.out.println("----------");
		ModelAndView mv = new ModelAndView("system/noticeAdd");
		String n_file = null;
		if (noticeFile != null) {
			String sufPath = "file";
			n_file = WebSourceToFileUtil.toFile(noticeFile, sufPath);
		}

		Employee acceptEmployee = new Employee();
		acceptEmployee.setId(2);
		Employee employee = new Employee();
		employee.setId(1);
		notice.setStatus(new Status(2, "通过"));
		notice.setN_file(n_file);

		noticeService.addNotice(notice);

		return mv;
	}

	// @RequestMapping("/my")
	public ModelAndView getNoticePage() {
		ModelAndView mv = new ModelAndView("system/noticeMy");
		// 查询所有的当前用户的页
		List<Notice> notices = noticeService.getNotices(1);
		mv.addObject("notices", notices);
		return mv;
	}

	@RequestMapping(value = { "/system/noticeMy", "/notice/my" })
	public ModelAndView getNoticeFixedPage(PageBean<Notice> pageBean) {
		System.out.println("pageBean"+pageBean);
		ModelAndView mv = new ModelAndView("system/noticeMy");
		PageBean<Notice> notices = noticeService.getFixedNotices(2, pageBean);
		mv.addObject("notices", notices);
		return mv;
	}
	
	@RequestMapping("/notice/my/{n_id}")
	public ModelAndView toModifyNoticePage(@PathVariable("n_id")int n_id) {
		System.out.println(n_id);
		ModelAndView mv = new ModelAndView("system/noticeDetail");
		Notice notice = noticeService.getNoticeByNid(n_id);
		mv.addObject("notice", notice);
		return mv;
	}
	
	@RequestMapping("/notice/detail")
	public ModelAndView modifyNotice(Notice notice, MultipartFile noticeFile) throws IllegalStateException, IOException {
		System.out.println("----------");
		System.out.println(noticeFile==null);
		System.out.println("1notice:"+notice);
		ModelAndView mv = new ModelAndView("system/noticeDetail");
		String n_file = null;
		if (noticeFile != null) {
			String sufPath = "file";
			n_file = WebSourceToFileUtil.toFile(noticeFile, sufPath);
		}

		Employee employee= employeeService.getEmployeeById(1);
		Employee acceptEmployee = employeeService.getEmployeeById(2);
		notice.setEmployee(employee);
		System.out.println("employee:"+employee);
		notice.setAcceptEmployee(acceptEmployee);
		System.out.println("acceptEmployee:"+acceptEmployee);
		notice.setStatus(new Status(2, "通过"));
		notice.setN_file(n_file);
		System.out.println("wwwwwwwwwwwwwwwwwwwwwwwww");
		noticeService.modifyNotice(notice);
		System.out.println("notice:"+notice);
		mv.addObject("notice", notice);
		return mv;
	}
	

}
